package com.example.learndevice.ui

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.learndevice.R
import com.example.learndevice.databinding.ActivityTakePictureBinding
import com.example.learndevice.widget.CameraView
import com.example.mydefinedview1.base.BaseActivity

class TakePictureActivity : BaseActivity<ActivityTakePictureBinding>(),View.OnClickListener {
    private var mTakeType = 0

    override fun ActivityTakePictureBinding.initBinding() {
        val cameraType = intent.getIntExtra("type",CameraView.CAMERA_BEHIND)
        mBinding.cameraView.setCameraType(cameraType)
        mBinding.btnShooting.setOnClickListener(this@TakePictureActivity)
        mBinding.btnShutter.setOnClickListener(this@TakePictureActivity)
    }

    override fun onBackPressed() {
        val intent = Intent()
        val bundle = Bundle()
        val photoPath = mBinding.cameraView.getPhotoPath()
        bundle.putInt("type",mTakeType)
        if (photoPath == null&&mTakeType == 0){
            bundle.putString("is_null","yes")
        }else{
            bundle.putString("is_null","no")
            if (mTakeType == 0){
                bundle.putString("path",photoPath)
            }else if (mTakeType == 1){
                bundle.putStringArrayList("path_list",mBinding.cameraView.getShootingList())
            }
        }
        intent.putExtras(bundle)
        setResult(Activity.RESULT_OK,intent)
        finish()
    }

    override fun onClick(v: View?) {
        when(v?.id){
            mBinding.btnShutter.id -> {
                mTakeType = 0
                mBinding.cameraView.doTakePicture()
            }
            mBinding.btnShooting.id -> {
                mTakeType = 1
                mBinding.cameraView.doTakeShooting()
            }
        }
    }
}